from window_frame import Ui_MainWindow
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
from custom_dialog import CustomDialog
from gitee import Gitee
from loguru import logger


class Window(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(QMainWindow, self).__init__()
        self.setupUi(self)
        self.__connect_signals()
        self.log_result = ''
        self._translate = QtCore.QCoreApplication.translate
        # 设置日志窗口不可编辑
        # self.plainTextEdit.setFocusPolicy(QtCore.Qt.NoFocus)

    def __connect_signals(self):
        self.choose_file.clicked.connect(self.__choose_file)
        self.show_dialog.clicked.connect(self.__show_dialog)
        self.show_edit_dialog.clicked.connect(self.__show_edit_text_dialog)
        self.show_custom_dialog.clicked.connect(self.__show_custom_dialog)
        self.test_write_log.clicked.connect(self.__write_test_log)
        # 监听菜单栏点击事件
        self.action1.triggered.connect(self.__click_menu_1)
        self.start_gitee.clicked.connect(self.__start_gitee)
        self.clear_log.clicked.connect(self.__clear_log)

    def __choose_file(self):
        filename, _ = QFileDialog.getOpenFileName(self, '打开文件', '.', "图像文件(*.jpg *.png)")
        self.__log(f"选中的文件地址 {filename}")

    def __show_dialog(self):
        self.__log('显示弹窗')
        # 普通提示框
        # reply = QMessageBox.information(self, '提示框', '这是提示消息!')
        # 自定义对话框按钮
        # self.reply = QMessageBox(QMessageBox.Information, '提示框', '这是提示消息!')
        # self.reply.addButton("确定", QMessageBox.YesRole)
        # self.reply.addButton('取消', QMessageBox.NoRole)
        # self.reply.show()
        # 运行错误对话框
        # reply = QMessageBox.critical(self, '提示', '出错了')
        # 消息对话框
        reply = QMessageBox.question(self, '提示', '是否退出?')
        if reply == QMessageBox.Yes:
            self.__log('点击确定')
        else:
            self.__log('点击取消')

    def __show_edit_text_dialog(self):
        value, ok = QInputDialog.getText(self, "标题", "输入提示信息", QLineEdit.Normal, '这是默认值')
        self.__log(f"获取到的输入框的值是: {value}")

    def __show_custom_dialog(self):
        self.custom_dialog = CustomDialog()
        self.custom_dialog.show()

    def __write_test_log(self):
        for i in range(1, 101):
            self.__log(f"这是第{i}条测试日志")

    def __click_menu_1(self):
        self.__log('点击菜单1')

    def __alert_error(self, msg):
        reply = QMessageBox.critical(self, '提示', msg)

    def __start_gitee(self):
        key = self.gitee_key.displayText()
        lang = self.gitee_lang.displayText()
        if len(key) <= 0:
            self.__alert_error('请输入搜索关键字')
        elif len(lang) <= 0:
            self.__alert_error('请输入要爬取的开发语言')
        else:
            self.gitee = Gitee(key, lang)
            self.gitee.signal.connect(self.__log)
            self.gitee.start()

    def __log(self, message):
        self.log_result += f"{message}\n"
        self.plainTextEdit.setPlainText(self._translate("MainWindow", self.log_result))
        self.plainTextEdit.verticalScrollBar().setValue(self.plainTextEdit.verticalScrollBar().maximum())
        logger.info(message)

    def __clear_log(self):
        self.log_result = ''
        self.plainTextEdit.setPlainText(self._translate("MainWindow", self.log_result))
        self.plainTextEdit.verticalScrollBar().setValue(self.plainTextEdit.verticalScrollBar().maximum())